package marble {

	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.controls.Alert;

	public class Locale {

		private var messages:XML;
		private var ready:Boolean = false;

		public function Locale(url:String) {

			var request:URLRequest = new URLRequest(url);
			var loader:URLLoader = new URLLoader();

			loader.addEventListener(Event.COMPLETE,
				function (event:Event):void {
					messages = new XML(loader.data);
					ready = true;
				}
			);

			try {
				loader.load(request);
			} catch (error:Error) {
				Alert.show("Unable to load language definitions.");
			}
		}

		public function translate(src:String):String {
			for each (var node:XML in messages..source) {
				if (node == src) {
					return node.parent().target;
				}
			}
			return src;
		}
		
		public function isReady():Boolean {
			return ready;
		}
	}
}